package service

import (
	"gitee.com/hp-huiw/family-expense-api/internal/dto"
	"gitee.com/hp-huiw/family-expense-api/internal/repository"
)

func ToIncomeExpenseDetailDTO(detail *repository.IncomeExpenseDetail) *dto.IncomeExpenseDetailResponse {
	var createdBy dto.UserAudit

	if detail.CreatedBy != nil {
		createdBy = dto.UserAudit{
			ID:        &detail.CreatedBy.ID,
			Username:  &detail.CreatedBy.Username,
			FullName:  &detail.CreatedBy.FullName,
			AvatarUrl: &detail.CreatedBy.AvatarUrl,
		}
	}

	var updatedBy dto.UserAudit

	if detail.UpdatedBy != nil {
		updatedBy = dto.UserAudit{
			ID:        &detail.UpdatedBy.ID,
			Username:  &detail.UpdatedBy.Username,
			FullName:  &detail.UpdatedBy.FullName,
			AvatarUrl: &detail.UpdatedBy.AvatarUrl,
		}
	}

	dto := &dto.IncomeExpenseDetailResponse{
		IncomeExpenseDetail: dto.IncomeExpenseDetail{
			FullAddressCoordinate: dto.FullAddressCoordinate{
				FormattedAddressCoordinate: dto.FormattedAddressCoordinate{
					Latitude:         detail.Latitude,
					Longitude:        detail.Longitude,
					FormattedAddress: detail.FormattedAddress,
				},
				Address:  detail.Address,
				Province: detail.Province,
				City:     detail.City,
				District: detail.District,
			},
			ID:          detail.ID,
			Amount:      detail.Amount,
			Type:        detail.Type,
			DetailType:  detail.DetailType,
			PaymentType: detail.PaymentType,
			Details:     detail.Details,
			Remark:      detail.Remark,
		},
		BankCardInfo: dto.BankCardInfo{
			BankID:               detail.BankID,
			BankShortName:        detail.BankShortName,
			BankCardName:         detail.BankCardName,
			BankCardEnabled:      detail.BankCardEnabled,
			BankCardIsCreditCard: detail.BankCardIsCreditCard,
		},
		AuditInfo: dto.AuditInfo{
			CreatedBy: createdBy,
			UpdatedBy: updatedBy,
			Timestamps: dto.Timestamps{
				DateCreated: detail.DateCreated,
				LastUpdated: detail.LastUpdated,
			},
		},
		BankCardID:        detail.BankCardID,
		IncomeExpenseDate: detail.IncomeExpenseDate,
	}

	if dto.BankShortName != nil {
		dto.BankLogoUrl = NewBankService(nil).GetLogoUrl(dto.BankShortName)
	}

	return dto
}
